<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class chungloai_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
		$this->load->library("unicode");
	}
	public function getDDL()
	{
		$query = $this->db->query("SELECT id, ten, tenUrl FROM chungloai WHERE anhien = 1 ORDER BY thutu");
		return $query->result();
	}
	public function select()
	{
		$query = $this->db->query("SELECT id, ten, thutu, anhien FROM chungloai ORDER BY thutu");
		return $query->result();
	}
	public function edit($id)
	{
		$query = $this->db->query("SELECT * FROM chungloai WHERE id = ?",array($id));
		return $query->result();
	}
	public function insert($ten, $thutu)
	{
		$tenUrl = $this->unicode->change($ten);
		if(is_numeric($thutu)) settype($thutu,"int"); else $thutu = 0;
		$this->db->query("INSERT INTO chungloai VALUES (NULL, ?, ?, ?, 1)", array($ten, $tenUrl, $thutu));
		return $this->db->insert_id();
	}
	public function update($id, $ten, $thutu, $anhien)
	{
		$tenUrl = $this->unicode->change($ten);
		$query = $this->db->query("UPDATE chungloai SET ten = ?, tenUrl = ?, thutu = ?, anhien = ? WHERE id = ?", array($ten, $tenUrl, $thutu, $anhien, $id));
		$chk = $this->db->query("SELECT id FROM chungloai WHERE ten = ? AND tenUrl = ? AND thutu = ? AND anhien = ? AND id = ?", array($ten, $tenUrl, $thutu, $anhien, $id));
		if($chk->num_rows() > 0) return TRUE; else return FALSE;
	}
	public function delete($id)
	{
		$this->db->query("DELETE FROM chungloai WHERE id = ?",array($id));
		$query = $this->db->query("SELECT id FROM chungloai WHERE id = ?",array($id));
		if($query->num_rows()>0) return FALSE; return TRUE;
	}
}